layui.use(['table','form','layer','laydate','laytpl','element'], function() {
	var table = layui.table;
	form = layui.form,
    $ = layui.jquery,
    laydate = layui.laydate,
    laytpl = layui.laytpl,
    element = layui.element;
	
	 //日期选择框
	laydate.render({
	    elem: '#inpbirthday'
	});
	
	laydate.render({
	    elem: '#inpentrytime'
	});
	
	/*加载学校类型下拉框*/
	$.ajax({
		type : 'get',
		url : '../json/schoolType.json',
		datatype : 'json',
		success : function(schooltypedate) {
			if (schooltypedate.code == 0) {
				var str = "";
				for(var i = 0; i < schooltypedate.count; i++){
					str += '<option value=' + schooltypedate.data[i].id + '>'+ schooltypedate.data[i].name+'</option>';
				}
				$("#selschooltype").append(str);
				form.render("select");
			}
		}
	});
	
	/*点击学校类型下拉框*/
	form.on('select(selschooltype)', function(data) {
		var schooltype = data.value;
		if(schooltype=="00" || schooltype==null || schooltype=='null'){
			schooltype="";
		}
		$("#selschool").html("");
		//根据阶段筛选学校
		$.ajax({
			type : 'get',
			url : '../sysschoolmanagement/getschoolbywhere?schooltype='+schooltype,
			datatype : 'json',
			success : function(schooldata) {
				if (schooldata.code == 0) {
					var str = "<option value='00'>请选择学校</option>";
					for(var i = 0; i < schooldata.count; i++){
						str += '<option value=' + schooldata.data[i].schoolid + '>'+ schooldata.data[i].schoolName +'</option>';
					}
					$("#selschool").append(str);
					form.render("select");
				}
				else{
					layer.msg('暂无对应的学校', {icon: 2});
					$("#selschool").html("");
					form.render("select");
				}
			}
		});
	});
	
	/*选择学校后加载角色下拉框*/
	form.on('select(selschool)', function(data) {
		var schoolid = data.value;
		if(schoolid==00 || schoolid==null || schoolid=='null'){
			layer.msg("请选择所属学校！");
			return;
		}
		$("#userType").html("");
		$.ajax({
			type : 'get',
			url : '../sysunifiedmanagement/getteacherrolelistbyschool?schoolid='+schoolid,
			datatype : 'json',
			success : function(roledata) {
				if (roledata.code == 0) {
					var str = "<option value='00'>请选择角色</option>";
					for(var i = 0; i < roledata.count; i++){
						str += '<option value=' + roledata.data[i].id + '>'+ roledata.data[i].name +'</option>';
					}
					$("#userType").append(str);
					form.render();
				}
				else{
					layer.msg(roledata.msg,{icon:2});
					$("#userType").html("");
					form.render();
				}
			}
		});
	});
	
	/*点击查询筛选教师*/
	$("#btnaselTeacher").click(function(){
		var schoolid=$("#selschool").val().trim();
		var roleid=$("#userType").val().trim();
		var userName=$("#seleUserName").val().trim();
		
		if(schoolid==00 || schoolid==null || schoolid=="" || schoolid=='null'){
			schoolid="";
		}
		if(roleid==00 || roleid==null || roleid=="" || roleid=='null'){
			roleid="";
		}
		table.render({
			elem: '#tabteacher',
			id:'teainfoid',
			url: '../systeachermanagement/getteacherbypage?userName=' + userName + "&roleid=" + roleid+'&schoolid='+schoolid,
			title: '教师数据表',
			height:"full-130",
			skin: 'line',
			even: true,
			cols: [
				[{
					field: 'userName',
					title: '账号',
					align: "center"
				}, {
					field: 'realName',
					title: '真实姓名',
					align: "center"
				}, {
					field: 'sex',
					title: '性别',
					align: "center"
				}, {
					field: 'birthday',
					title: '出生日期',
					align: "center"
				}, {
					field: 'email',
					title: '邮箱',
					align: "center"
				}, {
					field: 'phone',
					title: '联系电话',
					align: "center"
				},{
					field: 'schoolName',
					title: '所属学校',
					align: "center"
				}, {
					title: '操作',
					toolbar: '#barDemo',
					align: "center",
					width:'170'
				}]
			], page: {
				layout: ['prev', 'page', 'next', 'skip', 'count', 'limit'],
				groups: 5,
				limit: 10,
				theme: '#1E9FFF',						
			}
		});
	});
    
	/*操作栏*/
    table.on('tool(tabteacher)', function(obj){
	    var data = obj.data;
	    /* 查看教师信息详情*/
	    if(obj.event === 'btnselteacher'){
	    	$("#userName").text(data.userName);
	 		$("#realName").text(data.realName);
	 		$("#sex").text(data.sex);
	 		$("#entryTime").text(data.entryTime);
	 		$("#email").text(data.email);
	 		$("#phone").text(data.phone);
	 		$("#birthday").text(data.birthday);
	 		$("#graduateSchool").text(data.graduateSchool);
	 		$("#post").text(data.post);
	 		$("#tell").text(data.tell);
	 		$("#workTel").text(data.workTel);
	 		$("#address").text(data.address);
	 		
	 		layer.open({
		        type: 1, 
		        title: '教师信息详情',
		        area: ['800px', '430px'],
		        shade: 0.8,
		        content: $('#teacherdea'),
		        btn: ['返回'], 
		        yes: function(){
		          layer.closeAll();
		          $(".teacherdea-con").css("display","none");
		        },
		        cancel: function(){ 
				   $(".teacherdea-con").css("display","none");
				}
		    });
	    }
	    else if(obj.event === 'btneditteacher'){
	    	$("#teainfoid").val(data.teainfoid);
	    	$("#inpusername").val(data.userName);
			$("#inprealname").val(data.realName);
			$("#inpbirthday").val(data.birthday);
			$("#inpemail").val(data.email);
			$("#inpphone").val(data.phone);
			$("#inptell").val(data.tell);
			$("#inpworktel").val(data.workTel);
			$("#inpaddress").val(data.address);
			$("#inppost").val(data.post);
			$("#inpgraduateschool").val(data.graduateSchool);
			$("#inpentrytime").val(data.entryTime);
			$(".inpsex").each(function(){
				if($.trim($(".inpsex")[0].value) == $.trim(data.sex)){
					$(".inpsex")[0].checked = true;
					form.render("radio");
				}
				if($.trim($(".inpsex")[1].value) == $.trim(data.sex)){
					$(".inpsex")[1].checked = true;
					form.render("radio");
				}
			});
			var index=layer.open({
				type: 1, 
			    title: '修改教师信息',
		        area: ['830px', '95%'],
		        shade: 0.8,
		        maxmin: true,
		        content: $('#editFrom'),
		        cancel: function(){ 
				   $(".editFromlayer-con").css("display","none");
				}
		    });
	    	layer.full(index);
	    	
	    	/*保存修改*/
	        $("#btnedittea").click(function(){
	        	var teainfoid=$("#teainfoid").val();
	    		var realname=$("#inprealname").val();
	    		var sex=$("input[name='inpsex']:checked").val()
	    		var birthday=$("#inpbirthday").val();
	    		var email=$("#inpemail").val();
	    		var phone=$("#inpphone").val();
	    		var tell=$("#inptell").val();
	    		var worktel=$("#inpworktel").val();
	    		var address=$("#inpaddress").val();
	    		var post=$("#inppost").val();
	    		var graduateschool=$("#inpgraduateschool").val();
	    		var entrytime=$("#inpentrytime").val();
	    		
	    		$.ajax({
	    			url: '../systeachermanagement/updateteacher',  
	    	        type: 'get', 
	    	        dataType: 'json',  
	    	        data:{teainfoid:teainfoid,realName:realname,
	    	        	sex:sex,birthday:birthday,Email:email,phone:phone,
	    	        	tell:tell,workTel:worktel,address:address,post:post,
	    	        	graduateSchool:graduateschool,entryTime:entrytime},
	    	        success: function (getdata) {
	    	          if (getdata.code == 10001) {
	    	        	  layer.confirm("教师信息修改成功！", {
	    					  btn: ['确定'],
	    					  icon:1
	    				  }, function(){
	    					  table.reload("teainfoid", {
		    		                where: {
		    		                	keyword:getdata.code=='10001'
		    		                }
		    		          });
	    					  layer.closeAll();
	    				  });	
	    	          }else { 
	    	        	  layer.alert("教师信息修改失败！",2); 
	    	          }  
	    	        },
	    	        error:function(){
	    	        	layer.alert("教师修改失败！",2); 
	    	        }
	    		});
	        });
	    } 
	    /* 删除一个教师信息*/
	    else if(obj.event === 'btndelteacher'){
	    	layer.confirm('确定要删除么？', {
			  btn: ['确定','取消'],
			  icon:3
			}, function(){
				$.ajax({
        			url: '../systeachermanagement/deleteteacher',  
        	        type: 'get', 
        	        dataType: 'json',  
        	        data:{teainfoid:data.teainfoid},
        	        success: function (getdata) {
        	          if (getdata.code == 10001) { 
        	        	  layer.confirm(getdata.msg, {
	    					  btn: ['知道了'],
	    					  icon:1
	    				  },function(){
	    					  table.reload("teainfoid", { 
					              where: {
					            	  keyword:getdata.code == '10001'
					              }
	        	        	  });
	    					  layer.closeAll();
	    				  });
        	          }else { 
        	        	  layer.alert(getdata.msg, {icon: 2});
        	          }  
        	        },
        	        error:function(){}
        		}); 
			}, function(){ });
	    }
    });
});